/**
 * @Description : _upload.js
 * @Author : Tomi
 * @Email : lintaoming@qq.com
 * @Create : 2017/8/31
 */
(function($, window, undefined){
    'use strict';
    $(window.document).ready(function () {
        $('[dy8-unit="file"]').each(function(i, e){
            var $e = $(e),
                defaults = {
                    language:'zh',
                    showUpload: false,
                    showCaption: true,
                    autoReplace: true,
                    allowedFileExtensions: ['jpg','png','gif'],
                    maxFileCount:10,
                    enctype:'multipart/form-data',
                    msgNoFilesSelected:'上传文件错误，请核查！',
                    previewFileIcon: "<i class='glyphicon glyphicon-king'></i>"
                };

            if(typeof $e.data('initial-thumb') != 'undefined'){
                var initialPreview = [],
                    initialPreviewConfig = [];

                $.each(eval($e.data('initial-thumb')), function(i,v){
                    initialPreview.push(v.url);
                    initialPreviewConfig.push({
                        width: "120px",
                        key: v.key,
                        caption: (v.url.lastIndexOf('/') != -1) ? v.url.substring(v.url.lastIndexOf('/') + 1) : v.url
                    });
                });

                if(typeof $e.data('delete-url') != 'undefined'){
                    $.extend(defaults, {
                        initialPreviewShowDelete: true
                    });
                }else{
                    $.extend(defaults, {
                        initialPreviewShowDelete: false
                    });
                }
                $e.fileinput($.extend(defaults,{
                    language:'zh',
                    initialPreviewAsData: true,
                    initialPreview: initialPreview,
                    initialPreviewConfig: initialPreviewConfig,
                    overwriteInitial: false,
                    maxFileSize: 100,
                    msgNoFilesSelected: '上传文件错误，请核查！'
                }));
                return;
            }

            if(typeof $e.data('upload-url') != 'undefined'){
                $.extend(defaults, {
                    showUpload: true
                });
            }

            $e.fileinput(defaults);
        });
    });
})(jQuery, window);